#! /usr/bin/env python
#coding=utf-8
# 此方法用于生成寻路网格

import tgame_map
import iworld2d

class rect(object):
	def __init__(self, lu, ru, ld, rd):
		self.lu = lu
		self.ru = ru
		self.ld = ld
		self.rd = rd

def init():
	global g_allpos, g_allsize, possum, sizesum
	g_allpos = {}
	g_allsize = {}
	possum = 0
	sizesum = 0

	global g_rects
	g_rects = {}

	pass

def getnaline():
	global g_allpos, g_allsize, possum, sizesum, g_rects
	# 地形的所有可碰撞物体(不可移动矩形)
	for objn in tgame_map.g_objs:
		obj = tgame_map.g_objs[objn]
		if not obj.face:
			continue
		# 获取其位置,大小
		pos = obj.pos
		size = obj.face.size
		g_allpos[str(possum)] = pos
		g_allsize[str(possum)] = size
		possum += 1
		sizesum += 1
		print "pos:",pos," size:",size

		# 获取到的位置为左上角,目前该功能只做无旋转的物体
		# 计算也一个物体的4个顶点,连接成一个四边形
		lu = pos
		ru = (pos[0] + size[0], pos[1])
		ld = (pos[0], pos[1] + size[1])
		rd = (pos[0] + size[0], pos[1] + size[1])
		g_rects[str(possum)] = rect(lu, ru, ld, rd)

	for id in g_rects:
		print g_rects[id].ld, g_rects[id].rd, g_rects[id].ru, g_rects[id].lu
	pass

# 读取地图,获取其信息
def getmap(map_name):
	maps = __import__("tgame_map_%s"%map_name)
	global g_allpos, g_allsize, possum, sizesum, g_rects
	# 循环获取每一层场景 游戏场景固定物体一共4层
	icode = 1
	for obj in maps.data:
		map_obj = maps.data.get(obj)

		if map_obj["type"] != "floor" and map_obj["type"] != "za" and map_obj["type"] != "img_tile_p" and map_obj["type"] != "img_tile_p_b":
			continue

		img = iworld2d.image2d(map_obj["img"])
		pos = map_obj["pos"]
		size = img.size
		if map_obj["type"] == "img_tile_p" or map_obj["type"] == "img_tile_p_b":
			size = (img.size[0]*map_obj["w"], img.size[1] * map_obj["h"])
		g_allpos[str(possum)] = pos
		g_allsize[str(possum)] = size
		possum += 1
		sizesum += 1
		print "pos:",pos," size:",size

		# 获取到的位置为左上角,目前该功能只做无旋转的物体
		# 计算也一个物体的4个顶点,连接成一个四边形
		pc = 0
		#if icode%4 == 2:
		#	pc = 1
		#if icode%4 == 3:
		#	pc = 2
		#if icode%4 == 0:
		#	pc = 3
		lu = (pos[0] + pc, pos[1] + pc)
		ru = (pos[0] + size[0] + pc, pos[1] + pc)
		ld = (pos[0] + pc, pos[1] + size[1] + pc)
		rd = (pos[0] + size[0] + pc, pos[1] + size[1] + pc)
		g_rects[str(possum)] = rect(lu, ru, ld, rd)

		img.destroy()
		icode+=1

	for id in g_rects:
		print g_rects[id].ld, g_rects[id].rd, g_rects[id].ru, g_rects[id].lu

